package ru.st.selenium.applogic1;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import ru.st.selenium.util.Browser;

public class DriverBasedHelper {

  protected WebDriver driver;
  protected WebDriverWait wait;

  protected String gridHubUrl;

  protected String baseUrl;

  protected Browser browser;
	
  private boolean acceptNextAlert = true;
  
  public DriverBasedHelper(WebDriver driver) {
    this.driver = driver;
    wait = new WebDriverWait(driver, 10);
  }
  
  protected WebElement waitWebElement(By by) {
	return wait.until(ExpectedConditions.presenceOfElementLocated(by));
  }
  
  private boolean isElementPresent(By by) {
	try {
	  driver.findElement(by);
	  return true;
	} catch (NoSuchElementException e) {
	  return false;
	}
  }

  protected String closeAlertAndGetItsText() {
	try {
	  Alert alert = driver.switchTo().alert();
	  String alertText = alert.getText();
	  if (acceptNextAlert) {
	    alert.accept();
	  } else {
	    alert.dismiss();
	  }
	  return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }

  
}
